<?php

namespace app\models;

use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\data\Pagination;
use yii\data\ActiveDataProvider;
use yii\data\Sort;
use yii\helpers\Inflector;

class CustomTableViewSearch extends CustomTableView
{

    public $region_name;
    public $user_name;


    public static function tableName()
    {
        return CustomTableView::tableName();
    }

    public function rules()
    {
        return [
//            [['region_id'], 'safe'],
//            [['region_name'], 'safe'],
            [['remark'], 'safe'],


        ];
    }
//    public function search($params, $prod_spec_id=null)
//    {
//
//    }

    public function search($params, $custom_name='order', $spec_id='30001')
    {
        $where = [];
        $query = CustomTableView::find()->alias('a');
//        $query->joinWith('users  AS b');
//        $query->joinWith('custs  AS c');
//        $query->joinWith('prodInsts  AS d');
//        $query->joinWith('dealUsers  AS e');
//        $query->joinWith('orderTypes  AS f');
//        $query->joinWith('parentProdInsts  AS g');

        $where['a.custom_name'] = $custom_name;
        $where['a.spec_id'] = $spec_id;

        $query->where($where);

        $sort = new Sort([
            'attributes' => [
                'sort',
            ],
        ]);


        $query->orderBy($sort->orders);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'sort' => SORT_ASC,
                ]
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);


        if (!($this->load($params) )) {
            return $dataProvider;
        }

     //   $query->andFilterWhere(['like', 'a.remark', $this->remark]);

//        $query->andFilterWhere(['like', 'c.cust_name', $this->cust_name]);
//        $query->andFilterWhere(['like', 'd.prod_name', $this->prod_name]);
//
//        $query->andFilterWhere(['like', 'd.username', $this->user_name]);
//        $query->andFilterWhere(['like', 'e.username', $this->staffName]);

        return $dataProvider;


    }


}

?>
